<?php
class PostsController extends Controller
{
	public function addAction()
	{
		$model = new PostsModel();
		$model->dt_created = date('Y-m-d H:i:s');
		$model->dt_modify = '未修改';
		$model->access_count = mt_rand(50, 100);
		$category_id = (int) $this->request->getQuery('category_id', 0);
		$model->category_id = $category_id > 0 ? $category_id : 0;
		
		$this->save($model);
	}
	
	public function editAction()
	{
		$model = $this->loadModel();
		$this->save($model);
	}
	
	public function listAction()
	{
		$this->_trashOrRestoreOrDelete();
		$this->_list('!=');
	}
	
	public function trashAction()
	{
		$this->_trashOrRestoreOrDelete();
		$this->_list('=');
	}
	
	protected function _trashOrRestoreOrDelete()
	{
		$do = trim($this->request->getParam('do', ''));
		if ($do) 
		{
			$model = PostsModel::model();
			if (strcmp($do, 'delete') === 0)
			{
				$post_id = (int) $this->request->getParam('post_id', 0);
				if ($post_id > 0)
					$model->deleteByPk($post_id);
			}
			else if (strcmp($do, 'deleteAll') === 0)
			{
				$post_ids = $this->request->getParam('post_ids');
				if (is_array($post_ids))
				{
					foreach ($post_ids as $post_id)
						$model->deleteByPk($post_id);
				}
			}
			else if (in_array($do, array('t', 'o', 'c')))
			{
				$post_id = (int) $this->request->getParam('post_id', 0);
				if ($post_id > 0)
					$model->updateByPk($post_id, array('post_status' => $do));
			}
			else if (in_array($do, array('tAll', 'oAll', 'cAll')))
			{
				$post_ids = $this->request->getParam('post_ids');
				if (is_array($post_ids))
				{
					$post_status = substr($do, 0, 1);
					foreach ($post_ids as $post_id)
						$model->updateByPk($post_id, array('post_status' => $post_status));
				}
			}
		}
	}
	
	protected function _list($post_status = '!=')
	{
		$condition = '';
		$params = array();
		
		$condition .= 'post_status ' . $post_status . ' ?';
		$params[] = 't';
		
		$category_id = (int) $this->request->getParam('category_id');
		if ($category_id > 0)
		{
			$condition .= ' AND category_id = ?';
			$params[] = $category_id;
		}
		
		$post_flags = $this->request->getParam('post_flags', '');
		if (in_array($post_flags, array('c', 'h', 'j')))
		{
			$condition .= ' AND FIND_IN_SET(?, post_flags)';
			$params[] = $post_flags;
		}
		
		$post_status = $this->request->getParam('post_status');
		if (in_array($post_status, array('o', 'c')))
		{
			$condition .= ' AND post_status = ?';
			$params[] = $post_status;
		}
		
		$search = trim($this->request->getParam('search', ''));
		if (strlen($search) > 0)
		{
			$condition .= ' AND post_title LIKE ?';
			$params[] = '%' . $search . '%';
		}
		
		$model = PostsModel::model();
		$pagelist = $this->getPagelist($model->countByCondition($condition, $params));
		
		$order_by = trim($this->request->getParam('order_by', ''));
		$order_way = strtoupper($this->request->getParam('order_way', 'DSEC'));
		if (strcmp($order_way, 'DESC') !== 0)
			$order_way = 'ASC';
		
		if (strlen($order_by) > 0) 
			$condition .= ' ORDER BY ' . $order_by . ' ' . $order_way;
		
		$condition .= ' LIMIT ?, ?';
		$params[] = $pagelist->startResult;
		$params[] = $pagelist->perPageResults;
		
		$models = $model->findAll($condition, $params);
		
		$this->view->category_id = $category_id;
		$this->view->models = $models;
		$this->view->categories = CategoriesModel::model()->options(0, $category_id);
		
		$this->view->post_flags = $post_flags;
		$this->view->post_status = $post_status;
		$this->view->search = $search;
		$this->view->order_by = $order_by;
		$this->view->order_way = $order_way;
	}
	
	protected function save($model)
	{
		if ($attributes = $this->request->getPost('posts'))
		{
			$model->attributes = $attributes;
			if ($model->save())
			{
				$params = array(
					'id' => $model->pk,
					'm' => '已成功保存对该文章所作的修改：' . $model->post_title, 
					't' => self::T_MESSAGE
				);
				$do = $this->request->getPost('do', 'save');
				if (strcmp($do, 'save') === 0)
					$this->forward($params, 'list');
				else if (strcmp($do, 'apply') === 0)
					$this->forward($params, 'edit');
				else 
					$this->forward($params, 'add');
			}
			else
			{
				$_GET['m'] = '无法保存该文章的信息：' . $model->getError();
				$_GET['t'] = self::T_ERROR;
			}
		}
		
		$options = require $this->optionsDir;
		$this->view->thumb_width = $options['thumb_width'];
		$this->view->thumb_height = $options['thumb_height'];
		
		$this->view->model = $model;
		$this->view->categories = CategoriesModel::model()->options(0, $model->category_id);
		$this->fckeditor('posts[post_content]', 'posts', $model->post_content, 950, 550);
	}
	
	public function ajaxfileuploadAction()
	{
		$image = CImage::getInstance();
		$filename = 'source_' . mktime() . '_' . mt_rand(1000, 9999);
		$result = $msg = $filepath = '';
		if ($image->upload($_FILES['little_picture'], &$result, $this->uploadDir, $filename))
		{
			$filepath = str_replace('source_', 'thumb_', $result);
			$options = &$this->getOptions();
			$image->thumbnail($result, $filepath, $options['thumb_width'], $options['thumb_height']);
			if ($options['water_mark_status'] == 0)
			{
				$fontfile = CConfig::get('config', 'fontfile');
				$image->textWater($fontfile, $filepath, $options['water_mark_text'], $filepath, $options['water_mark_position']);	
			}
			else if ($options['water_mark_status'] == 1)
				$image->imageWater($filepath, ROOT_DIR . DS . $options['water_mark_dir'], $filepath, $options['water_mark_position'], $options['water_mark_pct']);
			
			$filepath = str_replace('\\', '/', $this->view->publicPath . strstr($filepath, '/uploads/'));
		}
		else 
			$msg = $result;
		
		$result = array('filepath' => $filepath, 'msg' => $msg);
		sleep(1);
		echo json_encode($result);
		exit;
	}
}
